package com.luoye.unionlotto;

import java.io.*;
import java.util.*;

public class UnionlottoFromFileGetter {
    protected String dataDir="";
    protected List<Unionlotto> unionlottoList;

    public UnionlottoFromFileGetter(String dataDir) {
        super();
        this.dataDir = dataDir;
        this.unionlottoList=new ArrayList<Unionlotto>();
    }
    public List<Unionlotto> get(){
        //读文件
        BufferedReader bufferedReader=null;
        try {
            bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(this.dataDir+"/data.txt")));
            String line=null;
            while ((line=bufferedReader.readLine())!=null){
                if(!line.equals("")) {
                    String date = line.substring(0, line.indexOf(":\t"));
                    String redBalls = line.substring(line.indexOf(":\t") + 2, line.indexOf("\t-\t"));
                    String blueBall = line.substring(line.indexOf("\t-\t") + 3);
                    Unionlotto unionlotto = new Unionlotto();
                    unionlotto.setDate(date);
                    List<String> redBallList = new ArrayList<String>();
                    redBallList.addAll(Arrays.asList(redBalls.split("\t")));
                    unionlotto.setRedBalls(redBallList);
                    unionlotto.setBlueBall(blueBall);
                    this.unionlottoList.add(unionlotto);
                }
            }
            System.out.println(unionlottoList);
            System.out.println(unionlottoList.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return this.unionlottoList;
    }
}
